{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q trino"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trino.dbapi import connect\n",
    "from trino.auth import OAuth2Authentication\n",
    "from redirect_handler import REDIRECT_HANDLER\n",
    "import urllib3\n",
    "import pandas as pd\n",
    "\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "\n",
    "TRINO_URI = \"https://trino-proxy:443\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use the Catalog (User 2: Anna)\n",
    "So far we have just used one user to query our data. We wouldn't need OPA for this.\n",
    "Lets setup a second user: Anna.\n",
    "\n",
    "1. Open a new private browser window, open `http://localhost:8181` (Lakekeeper UI) and login using Username `anna` and Password `iceberg`. Anna has no permissions yet. That's alright.\n",
    "2. Execute the following cell - copy the shown login link into the private browser you used before and re-login as `anna` if asked. The cell execution should fail, as Anna has no permissions yet.\n",
    "3. In your regular browser, navigate to the Warehouse \"demo\" and grant anna the \"select\" permission. Now re-run the cell below. Copy the login link to your private browser tab again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = connect(\n",
    "    host=TRINO_URI,\n",
    "    auth=OAuth2Authentication(REDIRECT_HANDLER),\n",
    "    http_scheme=\"https\",\n",
    "    verify=False,\n",
    "    catalog=\"lakekeeper\" # This line is new\n",
    ")\n",
    "\n",
    "cur = conn.cursor()\n",
    "# Execute query and fetch all rows\n",
    "cur.execute(\"SELECT * FROM finance.products\")\n",
    "rows = cur.fetchall()\n",
    "\n",
    "# Get column names\n",
    "columns = [desc[0] for desc in cur.description]\n",
    "\n",
    "# Create DataFrame\n",
    "df = pd.DataFrame(rows, columns=columns)\n",
    "\n",
    "# Display DataFrame\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, Trino enforces lakekeeper permissions via OPA!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
